<?php 

class mdl_account_limit extends ujn_model {
	
	function get($uid, $time = '') {
		$time = !empty($time) ? " and `acttime`='{$time}'" : '';
		$sql =<<<EOT
select * from `{$this->db_name}`.`{$this->tbl_name}`
where `uid` = '{$uid}' {$time} limit 1;
EOT;
		return $this->_db->getOne($sql);
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $uid
	 * @param unknown_type $field
	 */
	function inde($uid, $field, $is_incr = true) {
		
		$_incr = $is_incr ? '+' : '-';
		$_today = TODAY; // unix_timestamp(date(now())) != TODAY
		$sql =<<<EOT
update `{$this->db_name}`.`{$this->tbl_name}`
set `{$field}` = `{$field}` {$_incr} 1
where `uid` = '{$uid}' and `acttime` = '{$_today}';
EOT;
		$this->_db->query($sql);
		return $this->_db->affectedRows();
	}
	
	function init($uid, $pick, $wine) {
		$_today = TODAY; // unix_timestamp(date(now())) != TODAY
		$sql =<<<EOT
insert ignore `{$this->db_name}`.`{$this->tbl_name}`
set `uid` = '{$uid}', `acttime` = '{$_today}', `wine` = '{$wine}', `pick` = '{$pick}';
EOT;
		$this->_db->query($sql);
		return $this->_db->affectedRows();
	}
	
	protected function __construct() {
		parent::__construct(__CLASS__);
		$this->_db	= $this->mysql();
	}

    /**
     * @return mdl_account_limit
     */
	static function getInstance() {
		return parent::getInstance(__CLASS__);
	}
}